package com.fly.sms.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.fly.sms.util.C;
import com.fly.sms.util.JsonHttpClient;

/**
 * @author chenjr
 * 
 */
public class DriversService extends IService{
	public static List<Map<String, Object>> cache;

	public static List<Map<String, Object>> list(String pNo,String key) {
		cache = new ArrayList<Map<String, Object>>();
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", C.Act.Drivers_list);
		params.put("key", key);
		params.put("page", pNo);
		JSONArray ja = JsonHttpClient.post(params);
		if(ja==null){
			return null;
		}
		Log.d("json", ja.toString());
		int size = ja.length();
		for (int i = 0; i < size; i++) {
			JSONObject jo;
			try {
				jo = ja.getJSONObject(i);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", jo.getString("id"));
				map.put("name", jo.getString("name"));
				map.put("dirverNum", jo.getString("dirverNum"));
				map.put("idNum", jo.getString("idNum"));
				map.put("mobile", jo.getString("mobile"));
				map.put("address", jo.getString("address"));
				cache.add(map);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return cache;
	}
	
	public static int getCount(String key) {
		cache = new ArrayList<Map<String, Object>>();
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", C.Act.Drivers_count);
		params.put("key", key);
		JSONArray ja = JsonHttpClient.post(params);
		Log.d("json", ja.toString());
		int size = ja.length();
		for (int i = 0; i < size; i++) {
			JSONObject jo;
			try {
				jo = ja.getJSONObject(i);
				return jo.getInt("count");
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	
	public static Map<String, Object> get(String id){
		Map<String, Object> rst = new HashMap<String, Object>();
		Map<String, String> params = new HashMap<String, String>();
		params.put("act",C.Act.Drivers_get);
		params.put("id", id);
		JSONArray ja = JsonHttpClient.post(params);
		Log.d("json", ja.toString());
		int size = ja.length();
		for (int i = 0; i < size; i++) {
			JSONObject jo;
			try {
				jo = ja.getJSONObject(i);
				jo=jo.getJSONObject("drivers");
				rst.put("id", jo.getString("id"));
				rst.put("name", jo.getString("name"));
				rst.put("dirverNum", jo.getString("dirverNum"));
				rst.put("idNum", jo.getString("idNum"));
				rst.put("mobile", jo.getString("mobile"));
				rst.put("address", jo.getString("address"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return rst;
	}
}
